//
//  KIDBAWAdvertisementManager.m
//  VISIKARD
//
//  Created by Phan Ba Minh on 9/5/12.
//  Copyright (c) 2012 kidbaw. All rights reserved.
//

#import "KIDBAWWebAdvertisementManager.h"
#import "AppViewController.h"

@implementation KIDBAWWebAdvertisementManager

- (id)init
{
    self = [super init];
    if (self) {
        _webViewAds = [UIWebView new];
        _webViewAds.delegate = self;
        _webViewAds.backgroundColor = [UIColor colorWithRed:148.0/256.0 green:148.0/256.0 blue:148.0/256.0 alpha:0.0];
        _webViewAds.opaque = YES;
    }
    return self;
}

#pragma mark - Actions

- (void)startAdsInView:(UIViewController *)viewcontroller andBannerFrame:(CGRect)frame andDelegate:(id<KIDBAWAdvertisementProtocol>)del {
    _viewControllerAds = viewcontroller;
    _delegate = del;
    _frameShowedAds = frame;
    _timeShowedAds = CFAbsoluteTimeGetCurrent();
    _animationAds = enumKIDBAWAdvertisementAnimation_FromRightToLeft;
    
    [self loadInterface];
}

- (void)initAds {
    // BackGround
    if (_imgViewPlaceHolder) {
        [_imgViewPlaceHolder removeFromSuperview];
        _imgViewPlaceHolder = nil;
    }
    _imgViewPlaceHolder = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ads_placeholder.png"]];
    _imgViewPlaceHolder.frame = CGRectMake(_frameShowedAds.origin.x, _frameShowedAds.origin.y - 4, _frameShowedAds.size.width, _frameShowedAds.size.height + 4);
    [_viewControllerAds.view addSubview:_imgViewPlaceHolder];
    _imgViewPlaceHolder.hidden = YES;
    
    //********************************************
    // TODO:
//    //[_webViewAds loadHTMLString:@"" baseURL:[NSURL URLWithString:@"http://dev1.visikard.com/ARM2/web/openx/tag/user/522"]];
//    _webViewAds.frame = CGRectMake(0, [SupportFunction getDeviceHeight], [SupportFunction getDeviceWidth], HEIGHT_KIDBAW_ADVERTISEMENT_BANNER);
//    [_webViewAds loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://dev1.visikard.com/ARM2/web/openx/tag/user/%i",[UserDataManager Shared].userID]]]];
//    _webViewAds.backgroundColor = [UIColor clearColor];
//    _webViewAds.hidden = YES;
//    [_viewControllerAds.view addSubview:_webViewAds];
//    
//    UIView *dismissedView = [[UIView alloc] initWithFrame:CGRectMake(_frameShowedAds.origin.x, 0, _frameShowedAds.size.width, _frameShowedAds.size.height)];
//    [_webViewAds addSubview:dismissedView];
    //********************************************
    
//	// get localized path for file from app bundle
//	NSString *path;
//	NSBundle *thisBundle = [NSBundle mainBundle];
//	path = [thisBundle pathForResource:@"local_advertisement" ofType:@"html"];
}

- (void)loadInterface {
    [self initAds];
    [self allowAds];
}

- (void)allowAds {
    _imgViewPlaceHolder.hidden = NO;
    _webViewAds.hidden = NO;
    
    if (_isAllowed == enumKIDBAWAdvertisementIsAllowed_Prevented) {
        _isAllowed = enumKIDBAWAdvertisementIsAllowed_Invalid;
    }
}

- (void)preventAds {
    _imgViewPlaceHolder.hidden = YES;
    _webViewAds.hidden = YES;
    
    _isAllowed = enumKIDBAWAdvertisementIsAllowed_Prevented;
}

- (void)hideAds {
    _imgViewPlaceHolder.hidden = YES;
    _webViewAds.hidden = YES;
    
    _isAllowed = enumKIDBAWAdvertisementIsAllowed_Hidden;
}

- (void)refreshAds {
    NSLog(@"refreshAds");
    _statusAds = enumKIDBAWAdvertisementStatus_KIDBAW_Loading;
    
    [_webViewAds reload];
    
    [self hideAdsWithAnimation:_animationAds];
}

- (void)update
{
    if (CFAbsoluteTimeGetCurrent() - _timeReloadedAds > TIMER_RELOADING_WEB_BANNER && _statusAds != enumKIDBAWAdvertisementStatus_KIDBAW_Loading) {
        [self refreshAds];
    }
    
    if (_isAllowed != enumKIDBAWAdvertisementIsAllowed_Prevented) {
        [self hideAds];
    }
    else {
        if (_isAllowed != enumKIDBAWAdvertisementIsAllowed_Prevented) {
            [self allowAds];
        }
    }
}

#pragma mark - UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"webView shouldStartLoadWithRequest");
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"webViewDidFinishLoad");
    _viewInstanceOld = _viewInstanceNew;
    _viewInstanceNew = webView;
    _statusAds = enumKIDBAWAdvertisementStatus_KIDBAW_Finished;
    
    [self showAdsWithAnimation:_animationAds];
    
    _timeReloadedAds = CFAbsoluteTimeGetCurrent();
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"webView didFailLoadWithError");
    _statusAds = enumKIDBAWAdvertisementStatus_KIDBAW_Failed;
    _viewInstanceNew = _viewInstanceOld = nil;
    
    [self hideAdsWithAnimation:_animationAds];
    
    _timeReloadedAds = CFAbsoluteTimeGetCurrent();
}

@end
